/**
 * Created by Aigo on 2017/05/31.
 */
$(function () {
    var sel = $.Params.create();
    var sort = 102;
    getData();

    //添加
    $("#addDataBtn").on("click", function () {
        window.location.href = getIP() + "metLive/metLiveExpertAdd.html";
    })

    //删除
    $("#delBtn").on("click", function () {
        var params = getCheckData();
        if (params != "") {
            var del = $.Params.create();
            del.ajax("delete", getAdminIP() + "lives?params=" + params, function (data) {
                if (data.success) {
                    alert("删除成功！");
                    getData();
                } else {
                    alert("删除失败，请重试！");
                }
            })
        } else {
            alert("请选择要删除的项");
        }
    })

    //根据关键字，查询数据
    $("#searchBtn").on("click", function () {
        var searchVal = $("#searchVal").val();
        sel.addParams("pTitle", searchVal);
        getData();
    })

    //获取数据
    function getData() {
        sel.addParams("page", 0);
        sel.addParams("rows", rows);
        getPageData("GET", sel, getAdminIP() + "lives?sort=" + sort, function (data) {
            if (data.success) {
                var html = "";
                if (data.total > 0) {
                    $.each(data.result, function (i, val) {
                        var val = data.result[i];
                        var isClosed = "";
                        if (val.isClose == 0) {
                            isClosed = "已结束";
                        } else {
                            isClosed = "未结束";
                        }

                        html += "<tr role='row'>" +
                            "<td><input type='checkbox' eid='" + val.id + "' onclick='getCheck(this)' class='classOption'></td>" +
                            "<td><img src='../data/expertImg/" + val.expert_headImg + "' style='width:100%;' /></td>" +
                            "<td>" + val.expert_name + "</td>" +
                            "<td>" + val.expert_hospital + "</td>" +
                            "<td>" + val.title + "</td>" +
                            "<td>" + val.display_time + "</td>" +
                            "<td>" + val.end_time + "</td>" +
                            "<td>" + isClosed + "</td>" +
                            "<td><button type='button' class='btn btn-primary' onclick='update(\"" + val.id + "\")'>修改</button><button type='button' class='btn btn-primary' onclick='signUp(\"" + val.id + "\")'>报名记录</button></td>" +
                            "</tr>";
                    });
                    $("#listContainer").html(html);
                } else {
                    $("#listContainer").html("<tr><td colspan='9' class='txCenter'>暂无数据</td></tr>");

                }

            } else {
                $("#listContainer").html("<tr><td colspan='9' class='txCenter'>暂无数据</td></tr>");
            }
        })
    }

    //修改页面
    update = function (eid) {
        window.location.href = getIP() + "metLive/metLiveExpertUpdate.html?eid=" + eid;
    }

    //判断checkbox状态
    $("#selAll").on("click", function () {
        if (!$(this).attr("checked")) {
            $("input[type=checkbox]").attr("checked", "checked")
        } else {
            $("input[type=checkbox]").removeAttr("checked");
        }
    })
});
function signUp(eid) {
    window.location.href = getIP() + "metLive/metLiveSignUpMeetList.html?liveId=" + eid;
}